package com.xbongbong.saas.enums.dictionary;

import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.pro.statistic.enums.IndicatorRuleEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.ClueEnum;
import com.xbongbong.saas.enums.business.CommunicatePlanEnum;
import com.xbongbong.saas.enums.business.ContractEnum;
import com.xbongbong.saas.enums.business.CustomerCommunicateEnum;
import com.xbongbong.saas.enums.business.CustomerManagementEnum;
import com.xbongbong.saas.enums.business.InvoiceEnum;
import com.xbongbong.saas.enums.business.OtherIncomeEnum;
import com.xbongbong.saas.enums.business.OutstockEnum;
import com.xbongbong.saas.enums.business.PayPlanEnum;
import com.xbongbong.saas.enums.business.PaySheetEnum;
import com.xbongbong.saas.enums.business.PaymentSheetEnum;
import com.xbongbong.saas.enums.business.ProductEnum;
import com.xbongbong.saas.enums.business.ProductionOrderEnum;
import com.xbongbong.saas.enums.business.PurchaseEnum;
import com.xbongbong.saas.enums.business.RefundEnum;
import com.xbongbong.saas.enums.business.SalesOpportunityEnum;
import lombok.Getter;

import java.util.Objects;

/**
 * 数据字典类型枚举(总)
 * 保证字典type唯一
 * 防止以后有公司级和表单级数据字典迁移的 沙雕 需求
 *
 * 该份数据字典目前包含三部分：
 * （1）公司级别的：客户状态、合同状态
 * （2）表单级别的：客户阶段流失原因、销售阶段原因、延期原因
 * （3）表单下拉类型字典（在原saas内是数据字典）：客户行业、客户来源、客户类型、跟进方式、合同类型等
 * @author haibin.zhang
 * @version v1.0
 * @date 2019/2/19 14:17
 * @since v1.0
 */
@Getter
public enum DictionaryEnum {
    /*
      ---------------------------------------------------------------------------
      该部分为公司级别数据字典，即不与formId（表单id）关联
      ---------------------------------------------------------------------------
     */
    /**
     * 客户状态，在tb_data_dictionary表内，公司级别数据字典
     */
    CUSTOMER_STATUS(1, "enum.dictionary.name.customer_status", DictionaryTermEnum.DATA_DICTIONARY_TERM, CustomerManagementEnum.TYPE.getAttr(), CustomerManagementEnum.TYPE.getSaasAttr(), XbbRefTypeEnum.CUSTOMER_MANAGEMENT),
    /**
     * 合同状态，在tb_data_dictionary表内，公司级别数据字典
     */
    CONTRACT_STATUS(2, "enum.dictionary.name.contract_status", DictionaryTermEnum.DATA_DICTIONARY_TERM, ContractEnum.STATUS.getAttr(), ContractEnum.STATUS.getSaasAttr(), XbbRefTypeEnum.CONTRACT),
    /**
     * 物流公司
     */
    EXPRESS_NAME(119,"enum.dictionary.name.express_name",DictionaryTermEnum.DATA_DICTIONARY_TERM, OutstockEnum.EXPRESS_NAME.getAttr(), OutstockEnum.EXPRESS_NAME.getSaasAttr(), XbbRefTypeEnum.OUTSTOCK),

    /*
      ---------------------------------------------------------------------------
      该部分为表单类型数据字典，即根据formId的不同，会有不同的一套字典，必须限制formId
      ---------------------------------------------------------------------------
     */
    /**
     * 客户阶段流失原因，在tb_form_data_dictionary表内，必须限制formId
     */
    CUSTOMER_STAGE_WASTAGE(3, "enum.dictionary.name.customer_stage_wastage", DictionaryTermEnum.FORM_DATA_DICTIONARY_TERM, CustomerManagementEnum.WASTAGE.getAttr(), CustomerManagementEnum.WASTAGE.getSaasAttr(), XbbRefTypeEnum.CUSTOMER_MANAGEMENT),
    /**
     * 销售阶段流失原因，在tb_form_data_dictionary表内，必须限制formId
     */
    SALE_STAGE_WASTAGE(4, "enum.dictionary.name.sale_stage_wastage", DictionaryTermEnum.FORM_DATA_DICTIONARY_TERM, SalesOpportunityEnum.WASTAGE.getAttr(), SalesOpportunityEnum.WASTAGE.getSaasAttr(), XbbRefTypeEnum.SALES_OPPORTUNITY),
    /**
     * 延期原因，在tb_form_data_dictionary表内，必须限制formId
     */
    DELAY_CAUSE(5, "enum.dictionary.name.delay_cause", DictionaryTermEnum.FORM_DATA_DICTIONARY_TERM, CommunicatePlanEnum.DELAY_CAUSE.getAttr(), CommunicatePlanEnum.DELAY_CAUSE.getSaasAttr(), XbbRefTypeEnum.COMMUNICATE_PLAN),

    /*
      ---------------------------------------------------------------------------
      该部分为阶段相关，需要存入相应的tb_customer_stage、tb_sale_stage
      ---------------------------------------------------------------------------
     */
    CUSTOMER_STAGE(6, "enum.dictionary.name.customer_stage", DictionaryTermEnum.CUSTOMER_STAGE,
            CustomerManagementEnum.CUSTOMER_STAGE.getAttr(), CustomerManagementEnum.CUSTOMER_STAGE.getSaasAttr(), XbbRefTypeEnum.CUSTOMER_MANAGEMENT),
    SALE_STAGE(7, "enum.dictionary.name.sale_stage", DictionaryTermEnum.SALE_STAGE,
            SalesOpportunityEnum.SALE_STAGE.getAttr(), SalesOpportunityEnum.SALE_STAGE.getSaasAttr(), XbbRefTypeEnum.SALES_OPPORTUNITY),
    CLUE_STAGE(8, "enum.dictionary.name.clue_stage", DictionaryTermEnum.CLUE_STAGE,
            ClueEnum.CLUE_STAGE.getAttr(), ClueEnum.CLUE_STAGE.getSaasAttr(), XbbRefTypeEnum.CLUE),
    /*
      ---------------------------------------------------------------------------
       该部分为表单内下拉类型的字段，且在原saas模块时数据字典的一些字典，必须限制formId
      （1）分组dictionaryTermEnum都为PAAS_FORM_EXPLAIN_TERM，即都为表单的下拉选项型
      （2）主要为在原saas内是数据字典，但迁移到pro后变为表单的下拉选项的一些字段
      （3）非表单下拉字段的不要置入该处，如机会阶段客户阶段在相应的阶段表，不能置入此处
      ---------------------------------------------------------------------------
     */
    /**
     * 客户相关表单字典从50开始，必须限制formId
     */
    CUSTOMER_INDUSTRY(50, "enum.dictionary.name.customer_industry", DictionaryTermEnum.PAAS_FORM_EXPLAIN_TERM, CustomerManagementEnum.INDUSTRY.getAttr(), CustomerManagementEnum.INDUSTRY.getSaasAttr(), XbbRefTypeEnum.CUSTOMER_MANAGEMENT),
    CUSTOMER_SOURCE(51, "enum.dictionary.name.customer_source", DictionaryTermEnum.PAAS_FORM_EXPLAIN_TERM, CustomerManagementEnum.SOURCE.getAttr(), CustomerManagementEnum.SOURCE.getSaasAttr(), XbbRefTypeEnum.CUSTOMER_MANAGEMENT),
    CUSTOMER_GENRE(52, "enum.dictionary.name.customer_genre", DictionaryTermEnum.PAAS_FORM_EXPLAIN_TERM, CustomerManagementEnum.GENRE.getAttr(), CustomerManagementEnum.GENRE.getSaasAttr(), XbbRefTypeEnum.CUSTOMER_MANAGEMENT),
    CUSTOMER_IS_INDIVIDUAL(53, "enum.dictionary.name.customer_is_individual", DictionaryTermEnum.PAAS_FORM_EXPLAIN_TERM, CustomerManagementEnum.IS_INDIVIDUAL.getAttr(), CustomerManagementEnum.IS_INDIVIDUAL.getSaasAttr(), XbbRefTypeEnum.CUSTOMER_MANAGEMENT),
    CUSTOMER_COUNTRY(55, "enum.dictionary.name.customer_country", DictionaryTermEnum.PAAS_FORM_EXPLAIN_TERM, CustomerManagementEnum.COUNTRY.getAttr(), CustomerManagementEnum.COUNTRY.getSaasAttr(), XbbRefTypeEnum.CUSTOMER_MANAGEMENT),
    CUSTOMER_SCALE(56, "enum.dictionary.name.customer_scale", DictionaryTermEnum.PAAS_FORM_EXPLAIN_TERM, CustomerManagementEnum.SCALE.getAttr(), CustomerManagementEnum.SCALE.getSaasAttr(), XbbRefTypeEnum.CUSTOMER_MANAGEMENT),
    CUSTOMER_WASTAGE(57, "enum.dictionary.name.customer_wastage", DictionaryTermEnum.PAAS_FORM_EXPLAIN_TERM, CustomerManagementEnum.WASTAGE.getAttr(), CustomerManagementEnum.WASTAGE.getSaasAttr(), XbbRefTypeEnum.CUSTOMER_MANAGEMENT),

    /**
     * 跟进记录相关表单下拉选项从60开始，必须限制formId
     */
    COMMUNICATE_TYPE(60, "enum.dictionary.name.communicate_type", DictionaryTermEnum.PAAS_FORM_EXPLAIN_TERM, CustomerCommunicateEnum.TYPE.getAttr(), CustomerCommunicateEnum.TYPE.getSaasAttr(), XbbRefTypeEnum.CUSTOMER_COMMUNICATE),
    /**
     * 合同相关表单下拉选项从70开始，必须限制formId
     */
    CONTRACT_TYPE(70, "enum.dictionary.name.contract_type", DictionaryTermEnum.PAAS_FORM_EXPLAIN_TERM, ContractEnum.TYPE.getAttr(), ContractEnum.TYPE.getSaasAttr(), XbbRefTypeEnum.CONTRACT),
    /**
     * 退货退款单从80开始，必须限制formId
     */
    REFUND_REASON(80, "enum.dictionary.name.refund_reason", DictionaryTermEnum.PAAS_FORM_EXPLAIN_TERM, RefundEnum.REFUND_REASON.getAttr(), RefundEnum.REFUND_REASON.getSaasAttr(), XbbRefTypeEnum.REFUND),
    /**
     * 回款相关表单下拉选项从90开始，必须限制formId
     */
    //回款单支付方式
    PAYMENT_SHEET_PAY_METHOD(90, "enum.dictionary.name.payment_sheet_pay_method", DictionaryTermEnum.PAAS_FORM_EXPLAIN_TERM, PaymentSheetEnum.PAY_METHOD.getAttr(), PaymentSheetEnum.PAY_METHOD.getSaasAttr(), XbbRefTypeEnum.PAYMENT_SHEET),
    /**
     * 发票相关表单下拉选项从100开始，必须限制formId
     */
    INVOICE_TYPE(100, "enum.dictionary.name.invoice_type", DictionaryTermEnum.PAAS_FORM_EXPLAIN_TERM, InvoiceEnum.INVOICE_TYPE.getAttr(), InvoiceEnum.INVOICE_TYPE.getSaasAttr(), XbbRefTypeEnum.INVOICE),
    /**
     * 付款相关表单下拉选项从110开始，必须限制formId
     */
    //付款单支付方式
    PAY_SHEET_PAY_TYPE(110, "enum.dictionary.name.pay_sheet_pay_type", DictionaryTermEnum.PAAS_FORM_EXPLAIN_TERM, PaySheetEnum.PAY_TYPE.getAttr(), PaySheetEnum.PAY_TYPE.getSaasAttr(), XbbRefTypeEnum.PAY_SHEET),
    PAY_PLAN_METHOD(111, "enum.dictionary.name.pay_plan_method", DictionaryTermEnum.PAAS_FORM_EXPLAIN_TERM, PayPlanEnum.PAYMENT_METHOD.getAttr(), PayPlanEnum.PAYMENT_METHOD.getSaasAttr(), XbbRefTypeEnum.PAY_PLAN),
    /**方式
     * 采购合同相关表单下拉选项从120开始，必须限制formId
     */
    PURCHASE_STATUS(120, "enum.dictionary.name.purchase_status", DictionaryTermEnum.PAAS_FORM_EXPLAIN_TERM, PurchaseEnum.STATUS.getAttr(), PurchaseEnum.STATUS.getSaasAttr(), XbbRefTypeEnum.PURCHASE),
    /**
     * 生产单相关表单下拉选项从130开始，必须限制formId
     */
    PRODUCTION_ORDER_STATUS(130, "enum.dictionary.name.purchase_status", DictionaryTermEnum.PAAS_FORM_EXPLAIN_TERM, ProductionOrderEnum.STATUS.getAttr(), ProductionOrderEnum.STATUS.getSaasAttr(), XbbRefTypeEnum.PRODUCTION_ORDER),
    /**
     * 产品相关表单下拉选项从140开始，必须限制formId
     */
    PRODUCT_UNIT(140, "enum.dictionary.name.product_unit", DictionaryTermEnum.PAAS_FORM_EXPLAIN_TERM, ProductEnum.UNIT.getAttr(), ProductEnum.UNIT.getSaasAttr(), XbbRefTypeEnum.PRODUCT),

    /**
     * 线索阶段无效原因，在tb_form_data_dictionary表内，必须限制formId
     */
    CLUE_STAGE_INVALID(150, "enum.dictionary.name.clue_stage_invalid", DictionaryTermEnum.FORM_DATA_DICTIONARY_TERM, ClueEnum.INVALID_REASON.getAttr(), ClueEnum.INVALID_REASON.getSaasAttr(), XbbRefTypeEnum.CLUE),

    /**
     * 其他收入中的支付方式
     */
    OTHER_INCOME_PAY_METHOD(160, "enum.dictionary.name.other_income_pay_method", DictionaryTermEnum.PAAS_FORM_EXPLAIN_TERM, OtherIncomeEnum.PAY_METHOD.getAttr(), OtherIncomeEnum.PAY_METHOD.getSaasAttr(), XbbRefTypeEnum.OTHER_INCOME),

    /**
     * 其他支出中的支付方式
     */
    OTHER_EXPENSE_PAY_METHOD(170, "enum.dictionary.name.other_expense_pay_method", DictionaryTermEnum.PAAS_FORM_EXPLAIN_TERM, OtherIncomeEnum.PAY_METHOD.getAttr(), OtherIncomeEnum.PAY_METHOD.getSaasAttr(), XbbRefTypeEnum.OTHER_INCOME),

    UNKNOWN(404, "enum.dictionary.name.unknown", DictionaryTermEnum.UNKNOWN, "", "", XbbRefTypeEnum.UNKNOWN),
    ;
    private Integer type;
    private String name;
    /**
     * 用于区分不同的表
     */
    private DictionaryTermEnum dictionaryTermEnum;
    /**
     * 英文名称
     */
    private String attr;
    /**
     * 对应saas中的attr
     */
    private String saasAttr;
    /**
     * 对应saas中的业务类型枚举
     */
    private XbbRefTypeEnum xbbRefTypeEnum;

    DictionaryEnum(Integer type, String name, DictionaryTermEnum dictionaryTermEnum, String attr, String saasAttr, XbbRefTypeEnum xbbRefTypeEnum) {
        this.type = type;
        this.name = name;
        this.dictionaryTermEnum = dictionaryTermEnum;
        this.attr = attr;
        this.saasAttr = saasAttr;
        this.xbbRefTypeEnum = xbbRefTypeEnum;
    }

    /**
     * 根据code获取数据字典枚举
     * @param type 字段类型
     * @return com.xbongbong.saas.enums.dictionary.DictionaryEnum
     * @author zcp
     * @date 2019/3/4 15:14
     * @since v1.0
     * @version v1.0
     */
    public static DictionaryEnum getEnumByCode(Integer type) {
        for (DictionaryEnum cache : values()) {
            if (Objects.equals(type, cache.type)) {
                return cache;
            }
        }
        return UNKNOWN;
    }

    /**
     * 根据指标规则枚举获取出数据字典枚举（主要是客户行业、来源、状态等）
     * @param indicatorRuleEnum 指标规则枚举
     * @return com.xbongbong.saas.enums.dictionary.DictionaryEnum
     * @author zcp
     * @date 2019/5/1 17:26
     * @since v1.0
     * @version v1.0
     */
    public static DictionaryEnum getEnumByIndicatorRuleEnum(IndicatorRuleEnum indicatorRuleEnum){
        if (IndicatorRuleEnum.CUSTOMER_INDUSTRY == indicatorRuleEnum) {
            return CUSTOMER_INDUSTRY;
        } else if (IndicatorRuleEnum.CUSTOMER_SOURCE == indicatorRuleEnum) {
            return CUSTOMER_SOURCE;
        } else if (IndicatorRuleEnum.CUSTOMER_TYPE == indicatorRuleEnum) {
            return CUSTOMER_STATUS;
        } else if (IndicatorRuleEnum.CUSTOMER_LOSE_REASON == indicatorRuleEnum) {
            return CUSTOMER_WASTAGE;
        }
        return UNKNOWN;
    }

    public String getName() {
        return I18nMessageUtil.getMessage(name);
    }
}
